import request from "./request.js";
import common from "./common.js";

function payByOrderNo(orderNo) {
	return new Promise((resolve, reject)=> {
		request.get("/pay/prepay", {orderNo: orderNo}, {method: "post"}).then((result)=> {
			console.log(result, "==========")
			if(result.flag=='1') { //
				const dto = result.data;
				if(dto.flag!='1') { //生成统一订单出错
					const errMsg = dto.unifiedOrder.errCodeDes;
					uni.showModal({
						title: "支付通知",
						content: "结果信息："+errMsg,
						success() {
							common.reloadCur(); //如果出现错误，则刷新一下
						}
					})
					reject(errMsg);
				} else {
					if(dto.needMoney==0) {
						return resolve(dto.msg);
					} else {
						console.log("000000000000>>", dto)
						const from = common.checkFromSource();
						if(from==='wx') {
							uni.requestPayment({
								'nonceStr':dto.nonceStr,
								'timeStamp': dto.timeStamp,
								'package': dto.packageStr,
								'signType': dto.signType,
								'paySign': dto.paySign,
								'success':function(res){
									console.log(res, "==========>>>");
									showInfo(res);
									paySuccess(orderNo); //支付成功
									return resolve(res);
								},
								'fail':function(res){
									//console.log(res);
									showInfo(res);
									payFailed(orderNo); //支付失败
									return resolve(res);
								},
								'complete':function(res){
									// console.log("99999999999999999<<<", res)
								}
							})
						} else if(from === 'tt') {
							const packageObj = JSON.parse(dto.packageStr);
							// console.log("=====6666666666666666665===", packageObj);
							const orderInfo = {
								"order_id": packageObj['order_id'],
								'order_token': packageObj['order_token']
							};
							// console.log("---5--555555---", orderInfo);
							uni.requestPayment({
								'service':5,
								// '_debug': 1, //1表示是调试模式
								'orderInfo': orderInfo,
								'success':function(res){
									console.log(res, "==========>>>");
									const code = res.code;
									let suc = false;
									let codeTxt = '';
									if(code==0) {
										showInfo("支付成功", 'tt');
										paySuccess(orderNo); //支付成功
										suc = true;
										return resolve(res);
									} else if(code==1) {
										codeTxt = '支付超时';
									} else if(code==2) {
										codeTxt = '支付失败';
									} else if(code==3) {
										codeTxt = '支付关闭';
									} else if(code==4) {
										codeTxt = '支付取消';
									} else if(code==9) {
										codeTxt = '支付状态不确定';
									}
									if(!suc) {
										showInfo(codeTxt, 'tt');
										payFailed(orderNo); //支付失败
										return resolve(codeTxt);
									}
								},
								'fail':function(res){
									// console.log("支付失败：：：", res);
									showInfo(res, 'tt');
									payFailed(orderNo); //支付失败
									return resolve(res);
								},
								'complete':function(res){
									// console.log("99999999999999999<<<", res)
								}
							})
						}
					}
				}
				
			} else {
				let msg = result.data?result.data.msg:result.msg;
				if(!msg) {msg = result.msg;}
				uni.showModal({
					title: "支付通知",
					content: "生成支付订单失败【"+msg+"】",
					success() {
						if(result.data && result.data.code=='ORDERPAID') { //如果是订单已付款，则刷新
							common.reloadCur();
						}
					}
				})
				console.log(result);
				reject("生成支付订单失败");
			}
		})
	});
	
}

///支付成功调用
function paySuccess(orderNo) {
	request.get("/pay/payRes", {orderNo: orderNo, flag: "1"}, {method: "post"}).then((res)=> {
		
	})
}

function payFailed(orderNo) {
	request.get("/pay/payRes", {orderNo: orderNo, flag: "0"}, {method: "post"}).then((res)=> {
		
	})
}

function showInfo(data, from) {
	const msg = data.errMsg;
	let con = ((msg=='requestPayment:ok')?"支付成功":"支付失败");
	if(from==='tt') {
		con = data;
	}
	//console.log(msg)
	uni.showModal({
		title: '支付结果',
		content:con
	})
}


/**
 * 订单退款
 * @param {Object} ordersNo 订单号
 */
function refundOrders(ordersNo, reason) {
	return new Promise((resolve, reject)=> {
		request.get("miniOrdersService.refundOrders", {ordersNo: ordersNo, reason: reason}).then((res)=> {
			uni.showToast({
				title: res.message
			})
			if(res) {
				resolve(res.message);
			} else {reject(res.message)}
		})
	})
}

export default {
	payByOrderNo: payByOrderNo,
	refundOrders: refundOrders,
}